package yhao.infra.web.common.util;

import nl.basjes.parse.useragent.UserAgent;
import nl.basjes.parse.useragent.UserAgentAnalyzer;

/**
 * @author yoara
 * <p> Created on 2019/8/8 </p>
 */
public class UserAgentUtil {
    private static Object lock = new Object();
    //单例即可，本身使用同步方法，线程安全（可能影响速度，但问题不大）
    private UserAgentAnalyzer analyzer;
    private static UserAgentUtil instance;

    private UserAgentUtil() {
    }

    private static UserAgentUtil getInstance() {
        if (instance == null) {
            synchronized (lock) {
                if (instance == null) {
                    instance = new UserAgentUtil();
                    instance.analyzer = UserAgentAnalyzer
                            .newBuilder()
                            .hideMatcherLoadStats()
                            .withCache(1000)
                            .build();
                }
            }
        }
        return instance;
    }

    public static UserAgent analyzer(String userAgent){
        UserAgentAnalyzer uaa = getInstance().analyzer;
        return uaa.parse(userAgent);
    }
}
